package com.dao;
/***
*
*/
import com.util.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
public class NotesDao {
	public NotesDao(){
	
	}
	public int addNotes(HttpServletRequest req){
		String name=req.getParameter("name")==null?"":req.getParameter("name");
		String title=req.getParameter("title")==null?"":req.getParameter("title");
		String content=req.getParameter("content")==null?"":req.getParameter("content");
		String time=req.getParameter("time")==null?"":req.getParameter("time");
		String sql ="insert into TBL_NOTES("+
        		"NAME"+
        		",TITLE"+
        		",CONTENT"+
        		",TIME"+
        	") values("+
        		"'"+name+"'"+
        	        		",'"+title+"'"+
        	        		",'"+content+"'"+
        	        		",'"+Util.dateToString(new Date())+"'"+
        	")";
        	DB db = new DB();
        	return db.executeSql(sql);	
	}
	public int delNotes(HttpServletRequest req){
         String noteid=req.getParameter("noteid");
		String sql ="delete from TBL_NOTES where 1=1 "+
    		" and noteid ='"+noteid+"'"+
    	"";
    	DB db = new DB();
    	return db.executeSql(sql);	
	}
	public int updNotes(HttpServletRequest req){
		String name=req.getParameter("name")==null?"":req.getParameter("name");
		String title=req.getParameter("title")==null?"":req.getParameter("title");
		String content=req.getParameter("content")==null?"":req.getParameter("content");
		String time=req.getParameter("time")==null?"":req.getParameter("time");
        String noteid=req.getParameter("noteid");
		String sql ="update TBL_NOTES set "+
        		"NAME='"+name+"'"+
        		",TITLE='"+title+"'"+
        		",CONTENT='"+content+"'"+
        		 
        	" where 1=1 "+
    		" and noteid ='"+noteid+"'"+
    	"";
        	DB db = new DB();
        	return db.executeSql(sql);	
	}
	public Map<String,Object> findNotesById(HttpServletRequest req){
         String noteid=req.getParameter("noteid");
		String sql ="select * from TBL_NOTES where 1=1 "+
    		" and noteid ='"+noteid+"'"+
    	"";
    	DB db = new DB();
    	Map<String,Object> map = null;
    	List<Map<String,Object>> list = db.query(sql);
    	if(list!=null&&list.size()>0){
    		map = list.get(0);
    	}
    	return map;	
	}
	
	
	public PageModel findNotesByCondition(HttpServletRequest req){
		String name=req.getParameter("name")==null?"":req.getParameter("name");
		req.setAttribute("name", name);
		String title=req.getParameter("title")==null?"":req.getParameter("title");
		req.setAttribute("title", title);
		String content=req.getParameter("content")==null?"":req.getParameter("content");
		req.setAttribute("content", content);
		String time=req.getParameter("time")==null?"":req.getParameter("time");
		req.setAttribute("time", time);
         String noteid=req.getParameter("noteid");
		String sql ="";
		String where="";
		if(name!=null&&!"".equals(name)){
    		where+=" and NAME like '%"+name+"%'";
		}
		if(title!=null&&!"".equals(title)){
    		where+=" and TITLE like '%"+title+"%'";
		}
		if(content!=null&&!"".equals(content)){
    		where+=" and CONTENT like '%"+content+"%'";
		}
		if(time!=null&&!"".equals(time)){
    		where+=" and TIME like '%"+time+"%'";
		}
    	String countsql ="select count(*) from TBL_NOTES where 1=1 "+where;
    	PageModel model = new PageModel();
		String tempPage = req.getParameter("currentPage");
		int currentPage = 1;
		try {
			currentPage = Integer.parseInt(tempPage);
		} catch (NumberFormatException e) {
			currentPage=1;
		}
		model.setCurrentPage(currentPage);
		String tempCount = req.getParameter("pageCount");
		int pageCount = 20;
		try {
			pageCount = Integer.parseInt(tempCount);
		} catch (NumberFormatException e) {
			pageCount=20;
		}
		model.setPageCount(pageCount);
		sql="select  * from TBL_NOTES where 1=1 "+where+" order by  noteid  desc limit "+(currentPage-1)+","+pageCount+"";
    	DB db = new DB();
    	int count = Integer.parseInt(db.queryFirst(countsql));
    	model.setTotalCount(count);
    	if(count>0){
    		model.setRes(db.query(sql));
    	}
    	return model;	
	}
	
}